package com.rattsoftware.client.views;

import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.DepartmentSummary;
import com.rattsoftware.client.model.FacultySummary;
import com.rattsoftware.client.model.StaffSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.server.admin.model.Staff;

public class NewEditDepartmentView extends View {

	final AppMessages appMessages = GWT.create(AppMessages.class);

	// Course Unit Details
	private TextField<String> newDepartmentName;
	private TextField<String> facultyName;
	private HtmlEditor newDepartmentDescription;
	private ComboBox<StaffSummary> departmentHead;
	private TextField<String> noOfCourses;

	private DepartmentSummary departmentSummary;
	private FacultySummary facultySummary;
	
	private Window window;

	public NewEditDepartmentView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		GWT.log("NewEditDepartmentView : handleEvent");
		if (event.getType() == AppEvents.NewDepartment) {
			facultySummary = event.getData();
			createUI("Add New Department");
		} else if (event.getType() == AppEvents.EditDepartment) {
			departmentSummary = event.getData("department");
			facultySummary = event.getData("faculty");
			createUI("Edit Department Details for "
					+ departmentSummary.getDepartmentName());
		}
	}

	private void createUI(String title) {
		window = new Window();
		window.setHeading(title);
		window.setModal(true);
		window.setPlain(true);
		window.setClosable(false);
		window.setMaximizable(false);
		window.setDraggable(true);
		window.setResizable(false);
		window.setSize(745, 495);
		window.setScrollMode(Scroll.AUTOY);
		window.setLayout(new FitLayout());

		ProgressIndicator.showProgressBar();
		window.add(getDepartmentPanel());
		ProgressIndicator.hideProgressBar();
		window.show();
	}

	private FormPanel getDepartmentPanel() {
		FormData formData = new FormData("100%");

		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setSize(700, -1);
		panel.setLabelAlign(LabelAlign.TOP);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		LayoutContainer leftEditor = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		leftEditor.setLayout(layout);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		facultyName = new TextField<String>();
		facultyName.setFieldLabel("Faculty Name");
		facultyName.setEmptyText("Specify Faculty Name");

		if (facultySummary != null) {
			facultyName.setValue(facultySummary.getFacultyName());
		}
		facultyName.setEnabled(false);
		left.add(facultyName, formData);
		
		newDepartmentName = new TextField<String>();
		newDepartmentName.setFieldLabel("Department Name");
		newDepartmentName.setEmptyText("Specify Department Name");

		if (departmentSummary != null) {
			newDepartmentName.setValue(departmentSummary.getDepartmentName());
		}

		right.add(newDepartmentName, formData);

		departmentHead = new ComboBox<StaffSummary>();
		departmentHead.setFieldLabel("Department Head");
		departmentHead.setEmptyText("Select a staff..."); 
		departmentHead.setDisplayField("name");    
		departmentHead.setStore(getStaffStore());  
		departmentHead.setTypeAhead(true);  
		departmentHead.setTriggerAction(TriggerAction.ALL); 

		if (departmentSummary != null) {
			//departmentHead.setValue(courseUnitSummary.getCourseUnitName());
		}
		left.add(departmentHead, formData);

		noOfCourses = new TextField<String>();
		noOfCourses.setFieldLabel("No. Of Courses");

		if (departmentSummary != null) {
			noOfCourses.setValue(departmentSummary.getNoOfCourses());
		}

		right.add(noOfCourses, formData);

		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(main, new FormData("100%"));

		newDepartmentDescription = new HtmlEditor();
		newDepartmentDescription.setFieldLabel("About the Department");
		newDepartmentDescription.setHeight(290);
		newDepartmentDescription.setEmptyText("This can be anything that talks about this particular Department.");
		
		if (departmentSummary != null) {
			newDepartmentDescription.setValue(departmentSummary.getDepartmentDescription());
		}
		
		leftEditor.add(newDepartmentDescription, formData);

		panel.add(leftEditor, new FormData("100%"));

		Button buttonSave = new Button(appMessages.save());
		buttonSave.setIcon(IconHelper.createStyle("add"));
		buttonSave.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				// addNewCourse();
			}
		});

		Button buttonCancel = new Button(appMessages.cancel());
		buttonCancel.setIcon(IconHelper.createStyle("cancel"));
		buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				closeWindow();
			}
		});

		panel.addButton(buttonSave);
		panel.addButton(buttonCancel);

		FormButtonBinding binding = new FormButtonBinding(panel);
		binding.addButton(buttonSave);

		return panel;
	}

	@SuppressWarnings("deprecation")
	private void closeWindow() {
		departmentSummary = null;
		facultySummary = null;
		ProgressIndicator.showProgressBar();
		window.close();
		ProgressIndicator.hideProgressBar();
	}
	
	private ListStore<StaffSummary> getStaffStore() {
		ListStore<StaffSummary> staff_Store = new ListStore<StaffSummary>();  
		List<Staff> staffs = TestData.getStaff();
		
		for(Staff staff : staffs) {
			staff_Store.add(new StaffSummary(staff));  
		}
		return staff_Store;		
	}
}
